package com.guigu.dh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.guigu.dh.model.Department;
import com.guigu.dh.model.Hospital;
import com.guigu.dh.model.Schedule;
import com.guigu.dh.model.vo.CheckSignKeyVo;
import com.guigu.dh.service.HospitalSetService;
import com.guigu.dh.util.HttpClientRequestUtil;
import com.guigu.dh.util.MD5;
import com.guigu.dh.util.Result;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 这是医院设置数据的实现类
 * 发送httpClient请求，进行验证
 */
@Service
public class HospitalSetServiceImpl implements HospitalSetService {

    private final String URL = "http://localhost:8201";

    // 验证的方法
    @Override
    public Boolean checkSignKey(CheckSignKeyVo checkSignKeyVo) {
        checkSignKeyVo.setSignKey(MD5.encrypt(checkSignKeyVo.getSignKey()));
        // 将对象信息转为JSON字符串
        String s = JSONObject.toJSONString(checkSignKeyVo);

        String s1 = HttpClientRequestUtil.doPost(URL + "/api/hospital/checkSignKey",s,"UTF-8");

        // 转为对象
        Result result = JSONObject.parseObject(s1, Result.class);
        //System.out.println(result);
        return result.getCode() == 200;
    }

    // 添加的方法
    @Override
    public Result saveHospital(Hospital hospital) {
        // 先将密匙进行MD5加密
        hospital.setSignKey(MD5.encrypt(hospital.getSignKey()));
        String s = JSONObject.toJSONString(hospital);

        // 调用添加的api
        String result = HttpClientRequestUtil.doPost(URL + "/api/hospital/saveHospital", s, "UTF-8");

        // 返回结果
        Result result1 = JSONObject.parseObject(result, Result.class);

        return result1;
    }

    // 查询显示数据的方法
    @Override
    public Result showHospital(CheckSignKeyVo checkSignKeyVo) {
        // 因为MongoDB数据库存的是加密后的密匙，所以需要在这里先将密匙加密
        checkSignKeyVo.setSignKey(MD5.encrypt(checkSignKeyVo.getSignKey()));
        String s = JSONObject.toJSONString(checkSignKeyVo);
        // 调用查询api
        String s1 = HttpClientRequestUtil.doPost(URL + "/api/hospital/show", s, "UTF-8");
        // 转为对象后返回
        return JSONObject.parseObject(s1,Result.class);
    }

    // 分页查询科室的方法
    @Override
    public Result pagingDepartment(Map<String, String> map) {
        String s = JSONObject.toJSONString(map);
        String post = HttpClientRequestUtil.doPost(URL + "/api/hospital/pagingDepartment", s, "UTF-8");
        Result result = JSONObject.parseObject(post, Result.class);
        return result;
    }

    // 删除科室的方法
    @Override
    public Result delDepartmentByDepcode(String depcode) {
        // 发送delete请求
        String s = HttpClientRequestUtil.doDelete(URL + "/api/hospital/delDepartment/"+depcode);
        Result result = JSONObject.parseObject(s, Result.class);
        return result;
    }

    // 添加排班
    @Override
    public Boolean addSchedule(Schedule schedule) {
        // 转为JSON数据
        String s = JSONObject.toJSONString(schedule);

        // 调用api接口
        String str = HttpClientRequestUtil.doPost(URL + "/api/hospital/saveSchedule", s, "UTF-8");

        Result result = JSONObject.parseObject(str, Result.class);

        // 判断结果
        if (result.getCode() == 200) {
            return true;
        }
        return false;
    }

    // 分页排班
    @Override
    public Result pagingSchedule(Map<String, String> map) {
        String s = JSONObject.toJSONString(map);
        String post = HttpClientRequestUtil.doPost(URL + "/api/hospital/pagingSchedule", s, "UTF-8");
        Result result = JSONObject.parseObject(post, Result.class);
        return result;
    }

    // 删除排班
    @Override
    public Result delSchedule(String depcode, String hosScheduleId) {
        // 发送delete请求
        String s = HttpClientRequestUtil.doDelete(URL + "/api/hospital/delSchedule/"+depcode+"/"+hosScheduleId);
        Result result = JSONObject.parseObject(s, Result.class);
        return result;
    }

    // 添加科室的方法
    @Override
    public Boolean addDepartment(Department department) {
        // 转为JSON数据
        String s = JSONObject.toJSONString(department);

        // 调用api接口
        String str = HttpClientRequestUtil.doPost(URL + "/api/hospital/saveDepartment", s, "UTF-8");

        Result result = JSONObject.parseObject(str, Result.class);

        // 判断结果
        if (result.getCode() == 200) {
            return true;
        }
        return false;
    }
}
